########################################
### LOAD REQUIRED PACKAGES AND SCRIPTS
########################################

library(lavaan) # estimation of regression models
library(psych) # basic psych package
library(AICcmodavg) # computation of AICc values and Akaike weights
library(dplyr) # easy data manipulation
library(RSA) # plotting of response surfaces

##############################
### LOAD AND PREPARE DATA
##############################

# set working directory #

ERAselfknowledge.temp1 <- read.csv("Pilot 4 - self-insight into emotional abilities and life satisfaction.csv")
ERAselfknowledge.temp3 <- subset(ERAselfknowledge.temp1, consent==1)

# scoring the EI test
ERAselfknowledge.temp3$item1<- NA; ERAselfknowledge.temp3$item1[ERAselfknowledge.temp3$eraitem1==6] <- 1; ERAselfknowledge.temp3$item1[ERAselfknowledge.temp3$eraitem1!=6] <- 0
ERAselfknowledge.temp3$item2<- NA; ERAselfknowledge.temp3$item2[ERAselfknowledge.temp3$eraitem2==3] <- 1; ERAselfknowledge.temp3$item2[ERAselfknowledge.temp3$eraitem2!=3] <- 0
ERAselfknowledge.temp3$item3<- NA; ERAselfknowledge.temp3$item3[ERAselfknowledge.temp3$eraitem3==2] <- 1; ERAselfknowledge.temp3$item3[ERAselfknowledge.temp3$eraitem3!=2] <- 0
ERAselfknowledge.temp3$item4<- NA; ERAselfknowledge.temp3$item4[ERAselfknowledge.temp3$eraitem4==6] <- 1; ERAselfknowledge.temp3$item4[ERAselfknowledge.temp3$eraitem4!=6] <- 0
ERAselfknowledge.temp3$item5<- NA; ERAselfknowledge.temp3$item5[ERAselfknowledge.temp3$eraitem5==1] <- 1; ERAselfknowledge.temp3$item5[ERAselfknowledge.temp3$eraitem5!=1] <- 0
ERAselfknowledge.temp3$item6<- NA; ERAselfknowledge.temp3$item6[ERAselfknowledge.temp3$eraitem6==4] <- 1; ERAselfknowledge.temp3$item6[ERAselfknowledge.temp3$eraitem6!=4] <- 0
ERAselfknowledge.temp3$item7<- NA; ERAselfknowledge.temp3$item7[ERAselfknowledge.temp3$eraitem7==6] <- 1; ERAselfknowledge.temp3$item7[ERAselfknowledge.temp3$eraitem7!=6] <- 0
ERAselfknowledge.temp3$item8<- NA; ERAselfknowledge.temp3$item8[ERAselfknowledge.temp3$eraitem8==1] <- 1; ERAselfknowledge.temp3$item8[ERAselfknowledge.temp3$eraitem8!=1] <- 0
ERAselfknowledge.temp3$item9<- NA; ERAselfknowledge.temp3$item9[ERAselfknowledge.temp3$eraitem9==6] <- 1; ERAselfknowledge.temp3$item9[ERAselfknowledge.temp3$eraitem9!=6] <- 0
ERAselfknowledge.temp3$item10<- NA; ERAselfknowledge.temp3$item10[ERAselfknowledge.temp3$eraitem10==5] <- 1; ERAselfknowledge.temp3$item10[ERAselfknowledge.temp3$eraitem10!=5] <- 0
ERAselfknowledge.temp3$item11<- NA; ERAselfknowledge.temp3$item11[ERAselfknowledge.temp3$eraitem11==4] <- 1; ERAselfknowledge.temp3$item11[ERAselfknowledge.temp3$eraitem11!=4] <- 0
ERAselfknowledge.temp3$item12<- NA; ERAselfknowledge.temp3$item12[ERAselfknowledge.temp3$eraitem12==3] <- 1; ERAselfknowledge.temp3$item12[ERAselfknowledge.temp3$eraitem12!=3] <- 0
ERAselfknowledge.temp3$item13<- NA; ERAselfknowledge.temp3$item13[ERAselfknowledge.temp3$eraitem13==4] <- 1; ERAselfknowledge.temp3$item13[ERAselfknowledge.temp3$eraitem13!=4] <- 0
ERAselfknowledge.temp3$item14<- NA; ERAselfknowledge.temp3$item14[ERAselfknowledge.temp3$eraitem14==1] <- 1; ERAselfknowledge.temp3$item14[ERAselfknowledge.temp3$eraitem14!=1] <- 0
ERAselfknowledge.temp3$item15<- NA; ERAselfknowledge.temp3$item15[ERAselfknowledge.temp3$eraitem15==3] <- 1; ERAselfknowledge.temp3$item15[ERAselfknowledge.temp3$eraitem15!=3] <- 0
ERAselfknowledge.temp3$item16<- NA; ERAselfknowledge.temp3$item16[ERAselfknowledge.temp3$eraitem16==1] <- 1; ERAselfknowledge.temp3$item16[ERAselfknowledge.temp3$eraitem16!=1] <- 0
ERAselfknowledge.temp3$item17<- NA; ERAselfknowledge.temp3$item17[ERAselfknowledge.temp3$eraitem17==2] <- 1; ERAselfknowledge.temp3$item17[ERAselfknowledge.temp3$eraitem17!=2] <- 0
ERAselfknowledge.temp3$item18<- NA; ERAselfknowledge.temp3$item18[ERAselfknowledge.temp3$eraitem18==5] <- 1; ERAselfknowledge.temp3$item18[ERAselfknowledge.temp3$eraitem18!=5] <- 0
ERAselfknowledge.temp3$item19<- NA; ERAselfknowledge.temp3$item19[ERAselfknowledge.temp3$eraitem19==1] <- 1; ERAselfknowledge.temp3$item19[ERAselfknowledge.temp3$eraitem19!=1] <- 0
ERAselfknowledge.temp3$item20<- NA; ERAselfknowledge.temp3$item20[ERAselfknowledge.temp3$eraitem20==5] <- 1; ERAselfknowledge.temp3$item20[ERAselfknowledge.temp3$eraitem20!=5] <- 0
ERAselfknowledge.temp3$item21<- NA; ERAselfknowledge.temp3$item21[ERAselfknowledge.temp3$eraitem21==4] <- 1; ERAselfknowledge.temp3$item21[ERAselfknowledge.temp3$eraitem21!=4] <- 0
ERAselfknowledge.temp3$item22<- NA; ERAselfknowledge.temp3$item22[ERAselfknowledge.temp3$eraitem22==5] <- 1; ERAselfknowledge.temp3$item22[ERAselfknowledge.temp3$eraitem22!=5] <- 0
ERAselfknowledge.temp3$item23<- NA; ERAselfknowledge.temp3$item23[ERAselfknowledge.temp3$eraitem23==3] <- 1; ERAselfknowledge.temp3$item23[ERAselfknowledge.temp3$eraitem23!=3] <- 0
ERAselfknowledge.temp3$item24<- NA; ERAselfknowledge.temp3$item24[ERAselfknowledge.temp3$eraitem24==4] <- 1; ERAselfknowledge.temp3$item24[ERAselfknowledge.temp3$eraitem24!=4] <- 0
ERAselfknowledge.temp3$item25<- NA; ERAselfknowledge.temp3$item25[ERAselfknowledge.temp3$eraitem25==3] <- 1; ERAselfknowledge.temp3$item25[ERAselfknowledge.temp3$eraitem25!=3] <- 0
ERAselfknowledge.temp3$item26<- NA; ERAselfknowledge.temp3$item26[ERAselfknowledge.temp3$eraitem26==5] <- 1; ERAselfknowledge.temp3$item26[ERAselfknowledge.temp3$eraitem26!=5] <- 0
ERAselfknowledge.temp3$item27<- NA; ERAselfknowledge.temp3$item27[ERAselfknowledge.temp3$eraitem27==4] <- 1; ERAselfknowledge.temp3$item27[ERAselfknowledge.temp3$eraitem27!=4] <- 0
ERAselfknowledge.temp3$item28<- NA; ERAselfknowledge.temp3$item28[ERAselfknowledge.temp3$eraitem28==1] <- 1; ERAselfknowledge.temp3$item28[ERAselfknowledge.temp3$eraitem28!=1] <- 0
ERAselfknowledge.temp3$item29<- NA; ERAselfknowledge.temp3$item29[ERAselfknowledge.temp3$eraitem29==6] <- 1; ERAselfknowledge.temp3$item29[ERAselfknowledge.temp3$eraitem29!=6] <- 0
ERAselfknowledge.temp3$item30<- NA; ERAselfknowledge.temp3$item30[ERAselfknowledge.temp3$eraitem30==1] <- 1; ERAselfknowledge.temp3$item30[ERAselfknowledge.temp3$eraitem30!=1] <- 0
ERAselfknowledge.temp3$item31<- NA; ERAselfknowledge.temp3$item31[ERAselfknowledge.temp3$eraitem31==3] <- 1; ERAselfknowledge.temp3$item31[ERAselfknowledge.temp3$eraitem31!=3] <- 0
ERAselfknowledge.temp3$item32<- NA; ERAselfknowledge.temp3$item32[ERAselfknowledge.temp3$eraitem32==2] <- 1; ERAselfknowledge.temp3$item32[ERAselfknowledge.temp3$eraitem32!=2] <- 0
ERAselfknowledge.temp3$item33<- NA; ERAselfknowledge.temp3$item33[ERAselfknowledge.temp3$eraitem33==4] <- 1; ERAselfknowledge.temp3$item33[ERAselfknowledge.temp3$eraitem33!=4] <- 0
ERAselfknowledge.temp3$item34<- NA; ERAselfknowledge.temp3$item34[ERAselfknowledge.temp3$eraitem34==2] <- 1; ERAselfknowledge.temp3$item34[ERAselfknowledge.temp3$eraitem34!=2] <- 0
ERAselfknowledge.temp3$item35<- NA; ERAselfknowledge.temp3$item35[ERAselfknowledge.temp3$eraitem35==5] <- 1; ERAselfknowledge.temp3$item35[ERAselfknowledge.temp3$eraitem35!=5] <- 0
ERAselfknowledge.temp3$item36<- NA; ERAselfknowledge.temp3$item36[ERAselfknowledge.temp3$eraitem36==6] <- 1; ERAselfknowledge.temp3$item36[ERAselfknowledge.temp3$eraitem36!=6] <- 0
ERAselfknowledge.temp3$item37<- NA; ERAselfknowledge.temp3$item37[ERAselfknowledge.temp3$eraitem37==2] <- 1; ERAselfknowledge.temp3$item37[ERAselfknowledge.temp3$eraitem37!=2] <- 0
ERAselfknowledge.temp3$item38<- NA; ERAselfknowledge.temp3$item38[ERAselfknowledge.temp3$eraitem38==4] <- 1; ERAselfknowledge.temp3$item38[ERAselfknowledge.temp3$eraitem38!=4] <- 0
ERAselfknowledge.temp3$item39<- NA; ERAselfknowledge.temp3$item39[ERAselfknowledge.temp3$eraitem39==4] <- 1; ERAselfknowledge.temp3$item39[ERAselfknowledge.temp3$eraitem39!=4] <- 0
ERAselfknowledge.temp3$item40<- NA; ERAselfknowledge.temp3$item40[ERAselfknowledge.temp3$eraitem40==5] <- 1; ERAselfknowledge.temp3$item40[ERAselfknowledge.temp3$eraitem40!=5] <- 0
ERAselfknowledge.temp3$item41<- NA; ERAselfknowledge.temp3$item41[ERAselfknowledge.temp3$eraitem41==2] <- 1; ERAselfknowledge.temp3$item41[ERAselfknowledge.temp3$eraitem41!=2] <- 0
ERAselfknowledge.temp3$item42<- NA; ERAselfknowledge.temp3$item42[ERAselfknowledge.temp3$eraitem42==6] <- 1; ERAselfknowledge.temp3$item42[ERAselfknowledge.temp3$eraitem42!=6] <- 0
ERAselfknowledge.temp3$item43<- NA; ERAselfknowledge.temp3$item43[ERAselfknowledge.temp3$eraitem43==3] <- 1; ERAselfknowledge.temp3$item43[ERAselfknowledge.temp3$eraitem43!=3] <- 0
ERAselfknowledge.temp3$item44<- NA; ERAselfknowledge.temp3$item44[ERAselfknowledge.temp3$eraitem44==5] <- 1; ERAselfknowledge.temp3$item44[ERAselfknowledge.temp3$eraitem44!=5] <- 0
ERAselfknowledge.temp3$item45<- NA; ERAselfknowledge.temp3$item45[ERAselfknowledge.temp3$eraitem45==2] <- 1; ERAselfknowledge.temp3$item45[ERAselfknowledge.temp3$eraitem45!=2] <- 0
ERAselfknowledge.temp3$item46<- NA; ERAselfknowledge.temp3$item46[ERAselfknowledge.temp3$eraitem46==6] <- 1; ERAselfknowledge.temp3$item46[ERAselfknowledge.temp3$eraitem46!=6] <- 0
ERAselfknowledge.temp3$item47<- NA; ERAselfknowledge.temp3$item47[ERAselfknowledge.temp3$eraitem47==5] <- 1; ERAselfknowledge.temp3$item47[ERAselfknowledge.temp3$eraitem47!=5] <- 0
ERAselfknowledge.temp3$item48<- NA; ERAselfknowledge.temp3$item48[ERAselfknowledge.temp3$eraitem48==1] <- 1; ERAselfknowledge.temp3$item48[ERAselfknowledge.temp3$eraitem48!=1] <- 0
ERAselfknowledge.temp3$item49<- NA; ERAselfknowledge.temp3$item49[ERAselfknowledge.temp3$eraitem49==3] <- 1; ERAselfknowledge.temp3$item49[ERAselfknowledge.temp3$eraitem49!=3] <- 0
ERAselfknowledge.temp3$item50<- NA; ERAselfknowledge.temp3$item50[ERAselfknowledge.temp3$eraitem50==1] <- 1; ERAselfknowledge.temp3$item50[ERAselfknowledge.temp3$eraitem50!=1] <- 0
ERAselfknowledge.temp3$item51<- NA; ERAselfknowledge.temp3$item51[ERAselfknowledge.temp3$eraitem51==5] <- 1; ERAselfknowledge.temp3$item51[ERAselfknowledge.temp3$eraitem51!=5] <- 0
ERAselfknowledge.temp3$item52<- NA; ERAselfknowledge.temp3$item52[ERAselfknowledge.temp3$eraitem52==1] <- 1; ERAselfknowledge.temp3$item52[ERAselfknowledge.temp3$eraitem52!=1] <- 0
ERAselfknowledge.temp3$item53<- NA; ERAselfknowledge.temp3$item53[ERAselfknowledge.temp3$eraitem53==2] <- 1; ERAselfknowledge.temp3$item53[ERAselfknowledge.temp3$eraitem53!=2] <- 0
ERAselfknowledge.temp3$item54<- NA; ERAselfknowledge.temp3$item54[ERAselfknowledge.temp3$eraitem54==6] <- 1; ERAselfknowledge.temp3$item54[ERAselfknowledge.temp3$eraitem54!=6] <- 0
ERAselfknowledge.temp3$item55<- NA; ERAselfknowledge.temp3$item55[ERAselfknowledge.temp3$eraitem55==1] <- 1; ERAselfknowledge.temp3$item55[ERAselfknowledge.temp3$eraitem55!=1] <- 0
ERAselfknowledge.temp3$item56<- NA; ERAselfknowledge.temp3$item56[ERAselfknowledge.temp3$eraitem56==2] <- 1; ERAselfknowledge.temp3$item56[ERAselfknowledge.temp3$eraitem56!=2] <- 0
ERAselfknowledge.temp3$item57<- NA; ERAselfknowledge.temp3$item57[ERAselfknowledge.temp3$eraitem57==5] <- 1; ERAselfknowledge.temp3$item57[ERAselfknowledge.temp3$eraitem57!=5] <- 0
ERAselfknowledge.temp3$item58<- NA; ERAselfknowledge.temp3$item58[ERAselfknowledge.temp3$eraitem58==5] <- 1; ERAselfknowledge.temp3$item58[ERAselfknowledge.temp3$eraitem58!=5] <- 0
ERAselfknowledge.temp3$item59<- NA; ERAselfknowledge.temp3$item59[ERAselfknowledge.temp3$eraitem59==4] <- 1; ERAselfknowledge.temp3$item59[ERAselfknowledge.temp3$eraitem59!=4] <- 0
ERAselfknowledge.temp3$item60<- NA; ERAselfknowledge.temp3$item60[ERAselfknowledge.temp3$eraitem60==2] <- 1; ERAselfknowledge.temp3$item60[ERAselfknowledge.temp3$eraitem60!=2] <- 0
ERAselfknowledge.temp3$item61<- NA; ERAselfknowledge.temp3$item61[ERAselfknowledge.temp3$eraitem61==3] <- 1; ERAselfknowledge.temp3$item61[ERAselfknowledge.temp3$eraitem61!=3] <- 0
ERAselfknowledge.temp3$item62<- NA; ERAselfknowledge.temp3$item62[ERAselfknowledge.temp3$eraitem62==6] <- 1; ERAselfknowledge.temp3$item62[ERAselfknowledge.temp3$eraitem62!=6] <- 0
ERAselfknowledge.temp3$item63<- NA; ERAselfknowledge.temp3$item63[ERAselfknowledge.temp3$eraitem63==2] <- 1; ERAselfknowledge.temp3$item63[ERAselfknowledge.temp3$eraitem63!=2] <- 0
ERAselfknowledge.temp3$item64<- NA; ERAselfknowledge.temp3$item64[ERAselfknowledge.temp3$eraitem64==1] <- 1; ERAselfknowledge.temp3$item64[ERAselfknowledge.temp3$eraitem64!=1] <- 0
ERAselfknowledge.temp3$item65<- NA; ERAselfknowledge.temp3$item65[ERAselfknowledge.temp3$eraitem65==3] <- 1; ERAselfknowledge.temp3$item65[ERAselfknowledge.temp3$eraitem65!=3] <- 0
ERAselfknowledge.temp3$item66<- NA; ERAselfknowledge.temp3$item66[ERAselfknowledge.temp3$eraitem66==4] <- 1; ERAselfknowledge.temp3$item66[ERAselfknowledge.temp3$eraitem66!=4] <- 0
ERAselfknowledge.temp3$item67<- NA; ERAselfknowledge.temp3$item67[ERAselfknowledge.temp3$eraitem67==6] <- 1; ERAselfknowledge.temp3$item67[ERAselfknowledge.temp3$eraitem67!=6] <- 0
ERAselfknowledge.temp3$item68<- NA; ERAselfknowledge.temp3$item68[ERAselfknowledge.temp3$eraitem68==6] <- 1; ERAselfknowledge.temp3$item68[ERAselfknowledge.temp3$eraitem68!=6] <- 0
ERAselfknowledge.temp3$item69<- NA; ERAselfknowledge.temp3$item69[ERAselfknowledge.temp3$eraitem69==4] <- 1; ERAselfknowledge.temp3$item69[ERAselfknowledge.temp3$eraitem69!=4] <- 0
ERAselfknowledge.temp3$item70<- NA; ERAselfknowledge.temp3$item70[ERAselfknowledge.temp3$eraitem70==3] <- 1; ERAselfknowledge.temp3$item70[ERAselfknowledge.temp3$eraitem70!=3] <- 0
ERAselfknowledge.temp3$item71<- NA; ERAselfknowledge.temp3$item71[ERAselfknowledge.temp3$eraitem71==2] <- 1; ERAselfknowledge.temp3$item71[ERAselfknowledge.temp3$eraitem71!=2] <- 0
ERAselfknowledge.temp3$item72<- NA; ERAselfknowledge.temp3$item72[ERAselfknowledge.temp3$eraitem72==3] <- 1; ERAselfknowledge.temp3$item72[ERAselfknowledge.temp3$eraitem72!=3] <- 0

# calculate total score for EI test
ERAselfknowledge.temp3$ERAtotal<- with(ERAselfknowledge.temp3, (rowSums(cbind(item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14,item15,item16,item17,item18,item19,item20,item21,item22,item23,item24,item25,item26,item27,item28,item29,item30,item31,item32,item33,item34,item35,item36,item37,item38,item39,item40,item41,item42,item43,item44,item45,item46,item47,item48,item49,item50,item51,item52,item53,item54,item55,item56,item57,item58,item59,item60,item61,item62,item63,item64,item65,item66,item67,item68,item69,item70,item71,item72), na.rm=TRUE)))
ERAselfknowledge.temp3$ERApercent <- ERAselfknowledge.temp3$ERAtotal/72

# count how many items each person attempted
ERAselfknowledge.temp3$item1attempted <- NA; ERAselfknowledge.temp3$item1attempted[ERAselfknowledge.temp3$eraitem1>0] <- 1;
ERAselfknowledge.temp3$item2attempted <- NA; ERAselfknowledge.temp3$item2attempted[ERAselfknowledge.temp3$eraitem2>0] <- 1;
ERAselfknowledge.temp3$item3attempted <- NA; ERAselfknowledge.temp3$item3attempted[ERAselfknowledge.temp3$eraitem3>0] <- 1;
ERAselfknowledge.temp3$item4attempted <- NA; ERAselfknowledge.temp3$item4attempted[ERAselfknowledge.temp3$eraitem4>0] <- 1;
ERAselfknowledge.temp3$item5attempted <- NA; ERAselfknowledge.temp3$item5attempted[ERAselfknowledge.temp3$eraitem5>0] <- 1;
ERAselfknowledge.temp3$item6attempted <- NA; ERAselfknowledge.temp3$item6attempted[ERAselfknowledge.temp3$eraitem6>0] <- 1;
ERAselfknowledge.temp3$item7attempted <- NA; ERAselfknowledge.temp3$item7attempted[ERAselfknowledge.temp3$eraitem7>0] <- 1;
ERAselfknowledge.temp3$item8attempted <- NA; ERAselfknowledge.temp3$item8attempted[ERAselfknowledge.temp3$eraitem8>0] <- 1;
ERAselfknowledge.temp3$item9attempted <- NA; ERAselfknowledge.temp3$item9attempted[ERAselfknowledge.temp3$eraitem9>0] <- 1;
ERAselfknowledge.temp3$item10attempted <- NA; ERAselfknowledge.temp3$item10attempted[ERAselfknowledge.temp3$eraitem10>0] <- 1;
ERAselfknowledge.temp3$item11attempted <- NA; ERAselfknowledge.temp3$item11attempted[ERAselfknowledge.temp3$eraitem11>0] <- 1;
ERAselfknowledge.temp3$item12attempted <- NA; ERAselfknowledge.temp3$item12attempted[ERAselfknowledge.temp3$eraitem12>0] <- 1;
ERAselfknowledge.temp3$item13attempted <- NA; ERAselfknowledge.temp3$item13attempted[ERAselfknowledge.temp3$eraitem13>0] <- 1;
ERAselfknowledge.temp3$item14attempted <- NA; ERAselfknowledge.temp3$item14attempted[ERAselfknowledge.temp3$eraitem14>0] <- 1;
ERAselfknowledge.temp3$item15attempted <- NA; ERAselfknowledge.temp3$item15attempted[ERAselfknowledge.temp3$eraitem15>0] <- 1;
ERAselfknowledge.temp3$item16attempted <- NA; ERAselfknowledge.temp3$item16attempted[ERAselfknowledge.temp3$eraitem16>0] <- 1;
ERAselfknowledge.temp3$item17attempted <- NA; ERAselfknowledge.temp3$item17attempted[ERAselfknowledge.temp3$eraitem17>0] <- 1;
ERAselfknowledge.temp3$item18attempted <- NA; ERAselfknowledge.temp3$item18attempted[ERAselfknowledge.temp3$eraitem18>0] <- 1;
ERAselfknowledge.temp3$item19attempted <- NA; ERAselfknowledge.temp3$item19attempted[ERAselfknowledge.temp3$eraitem19>0] <- 1;
ERAselfknowledge.temp3$item20attempted <- NA; ERAselfknowledge.temp3$item20attempted[ERAselfknowledge.temp3$eraitem20>0] <- 1;
ERAselfknowledge.temp3$item21attempted <- NA; ERAselfknowledge.temp3$item21attempted[ERAselfknowledge.temp3$eraitem21>0] <- 1;
ERAselfknowledge.temp3$item22attempted <- NA; ERAselfknowledge.temp3$item22attempted[ERAselfknowledge.temp3$eraitem22>0] <- 1;
ERAselfknowledge.temp3$item23attempted <- NA; ERAselfknowledge.temp3$item23attempted[ERAselfknowledge.temp3$eraitem23>0] <- 1;
ERAselfknowledge.temp3$item24attempted <- NA; ERAselfknowledge.temp3$item24attempted[ERAselfknowledge.temp3$eraitem24>0] <- 1;
ERAselfknowledge.temp3$item25attempted <- NA; ERAselfknowledge.temp3$item25attempted[ERAselfknowledge.temp3$eraitem25>0] <- 1;
ERAselfknowledge.temp3$item26attempted <- NA; ERAselfknowledge.temp3$item26attempted[ERAselfknowledge.temp3$eraitem26>0] <- 1;
ERAselfknowledge.temp3$item27attempted <- NA; ERAselfknowledge.temp3$item27attempted[ERAselfknowledge.temp3$eraitem27>0] <- 1;
ERAselfknowledge.temp3$item28attempted <- NA; ERAselfknowledge.temp3$item28attempted[ERAselfknowledge.temp3$eraitem28>0] <- 1;
ERAselfknowledge.temp3$item29attempted <- NA; ERAselfknowledge.temp3$item29attempted[ERAselfknowledge.temp3$eraitem29>0] <- 1;
ERAselfknowledge.temp3$item30attempted <- NA; ERAselfknowledge.temp3$item30attempted[ERAselfknowledge.temp3$eraitem30>0] <- 1;
ERAselfknowledge.temp3$item31attempted <- NA; ERAselfknowledge.temp3$item31attempted[ERAselfknowledge.temp3$eraitem31>0] <- 1;
ERAselfknowledge.temp3$item32attempted <- NA; ERAselfknowledge.temp3$item32attempted[ERAselfknowledge.temp3$eraitem32>0] <- 1;
ERAselfknowledge.temp3$item33attempted <- NA; ERAselfknowledge.temp3$item33attempted[ERAselfknowledge.temp3$eraitem33>0] <- 1;
ERAselfknowledge.temp3$item34attempted <- NA; ERAselfknowledge.temp3$item34attempted[ERAselfknowledge.temp3$eraitem34>0] <- 1;
ERAselfknowledge.temp3$item35attempted <- NA; ERAselfknowledge.temp3$item35attempted[ERAselfknowledge.temp3$eraitem35>0] <- 1;
ERAselfknowledge.temp3$item36attempted <- NA; ERAselfknowledge.temp3$item36attempted[ERAselfknowledge.temp3$eraitem36>0] <- 1;
ERAselfknowledge.temp3$item37attempted <- NA; ERAselfknowledge.temp3$item37attempted[ERAselfknowledge.temp3$eraitem37>0] <- 1;
ERAselfknowledge.temp3$item38attempted <- NA; ERAselfknowledge.temp3$item38attempted[ERAselfknowledge.temp3$eraitem38>0] <- 1;
ERAselfknowledge.temp3$item39attempted <- NA; ERAselfknowledge.temp3$item39attempted[ERAselfknowledge.temp3$eraitem39>0] <- 1;
ERAselfknowledge.temp3$item40attempted <- NA; ERAselfknowledge.temp3$item40attempted[ERAselfknowledge.temp3$eraitem40>0] <- 1;
ERAselfknowledge.temp3$item41attempted <- NA; ERAselfknowledge.temp3$item41attempted[ERAselfknowledge.temp3$eraitem41>0] <- 1;
ERAselfknowledge.temp3$item42attempted <- NA; ERAselfknowledge.temp3$item42attempted[ERAselfknowledge.temp3$eraitem42>0] <- 1;
ERAselfknowledge.temp3$item43attempted <- NA; ERAselfknowledge.temp3$item43attempted[ERAselfknowledge.temp3$eraitem43>0] <- 1;
ERAselfknowledge.temp3$item44attempted <- NA; ERAselfknowledge.temp3$item44attempted[ERAselfknowledge.temp3$eraitem44>0] <- 1;
ERAselfknowledge.temp3$item45attempted <- NA; ERAselfknowledge.temp3$item45attempted[ERAselfknowledge.temp3$eraitem45>0] <- 1;
ERAselfknowledge.temp3$item46attempted <- NA; ERAselfknowledge.temp3$item46attempted[ERAselfknowledge.temp3$eraitem46>0] <- 1;
ERAselfknowledge.temp3$item47attempted <- NA; ERAselfknowledge.temp3$item47attempted[ERAselfknowledge.temp3$eraitem47>0] <- 1;
ERAselfknowledge.temp3$item48attempted <- NA; ERAselfknowledge.temp3$item48attempted[ERAselfknowledge.temp3$eraitem48>0] <- 1;
ERAselfknowledge.temp3$item49attempted <- NA; ERAselfknowledge.temp3$item49attempted[ERAselfknowledge.temp3$eraitem49>0] <- 1;
ERAselfknowledge.temp3$item50attempted <- NA; ERAselfknowledge.temp3$item50attempted[ERAselfknowledge.temp3$eraitem50>0] <- 1;
ERAselfknowledge.temp3$item51attempted <- NA; ERAselfknowledge.temp3$item51attempted[ERAselfknowledge.temp3$eraitem51>0] <- 1;
ERAselfknowledge.temp3$item52attempted <- NA; ERAselfknowledge.temp3$item52attempted[ERAselfknowledge.temp3$eraitem52>0] <- 1;
ERAselfknowledge.temp3$item53attempted <- NA; ERAselfknowledge.temp3$item53attempted[ERAselfknowledge.temp3$eraitem53>0] <- 1;
ERAselfknowledge.temp3$item54attempted <- NA; ERAselfknowledge.temp3$item54attempted[ERAselfknowledge.temp3$eraitem54>0] <- 1;
ERAselfknowledge.temp3$item55attempted <- NA; ERAselfknowledge.temp3$item55attempted[ERAselfknowledge.temp3$eraitem55>0] <- 1;
ERAselfknowledge.temp3$item56attempted <- NA; ERAselfknowledge.temp3$item56attempted[ERAselfknowledge.temp3$eraitem56>0] <- 1;
ERAselfknowledge.temp3$item57attempted <- NA; ERAselfknowledge.temp3$item57attempted[ERAselfknowledge.temp3$eraitem57>0] <- 1;
ERAselfknowledge.temp3$item58attempted <- NA; ERAselfknowledge.temp3$item58attempted[ERAselfknowledge.temp3$eraitem58>0] <- 1;
ERAselfknowledge.temp3$item59attempted <- NA; ERAselfknowledge.temp3$item59attempted[ERAselfknowledge.temp3$eraitem59>0] <- 1;
ERAselfknowledge.temp3$item60attempted <- NA; ERAselfknowledge.temp3$item60attempted[ERAselfknowledge.temp3$eraitem60>0] <- 1;
ERAselfknowledge.temp3$item61attempted <- NA; ERAselfknowledge.temp3$item61attempted[ERAselfknowledge.temp3$eraitem61>0] <- 1;
ERAselfknowledge.temp3$item62attempted <- NA; ERAselfknowledge.temp3$item62attempted[ERAselfknowledge.temp3$eraitem62>0] <- 1;
ERAselfknowledge.temp3$item63attempted <- NA; ERAselfknowledge.temp3$item63attempted[ERAselfknowledge.temp3$eraitem63>0] <- 1;
ERAselfknowledge.temp3$item64attempted <- NA; ERAselfknowledge.temp3$item64attempted[ERAselfknowledge.temp3$eraitem64>0] <- 1;
ERAselfknowledge.temp3$item65attempted <- NA; ERAselfknowledge.temp3$item65attempted[ERAselfknowledge.temp3$eraitem65>0] <- 1;
ERAselfknowledge.temp3$item66attempted <- NA; ERAselfknowledge.temp3$item66attempted[ERAselfknowledge.temp3$eraitem66>0] <- 1;
ERAselfknowledge.temp3$item67attempted <- NA; ERAselfknowledge.temp3$item67attempted[ERAselfknowledge.temp3$eraitem67>0] <- 1;
ERAselfknowledge.temp3$item68attempted <- NA; ERAselfknowledge.temp3$item68attempted[ERAselfknowledge.temp3$eraitem68>0] <- 1;
ERAselfknowledge.temp3$item69attempted <- NA; ERAselfknowledge.temp3$item69attempted[ERAselfknowledge.temp3$eraitem69>0] <- 1;
ERAselfknowledge.temp3$item70attempted <- NA; ERAselfknowledge.temp3$item70attempted[ERAselfknowledge.temp3$eraitem70>0] <- 1;
ERAselfknowledge.temp3$item71attempted <- NA; ERAselfknowledge.temp3$item71attempted[ERAselfknowledge.temp3$eraitem71>0] <- 1;
ERAselfknowledge.temp3$item72attempted <- NA; ERAselfknowledge.temp3$item72attempted[ERAselfknowledge.temp3$eraitem72>0] <- 1;

# calculate total attempted for each participant
ERAselfknowledge.temp3$ERAtotalattempted<- with(ERAselfknowledge.temp3, (rowSums(cbind(item1attempted,item2attempted,item3attempted,item4attempted,item5attempted,item6attempted,item7attempted,item8attempted,item9attempted,item10attempted,item11attempted,item12attempted,item13attempted,item14attempted,item15attempted,item16attempted,item17attempted,item18attempted,item19attempted,item20attempted,item21attempted,item22attempted,item23attempted,item24attempted,item25attempted,item26attempted,item27attempted,item28attempted,item29attempted,item30attempted,item31attempted,item32attempted,item33attempted,item34attempted,item35attempted,item36attempted,item37attempted,item38attempted,item39attempted,item40attempted,item41attempted,item42attempted,item43attempted,item44attempted,item45attempted,item46attempted,item47attempted,item48attempted,item49attempted,item50attempted,item51attempted,item52attempted,item53attempted,item54attempted,item55attempted,item56attempted,item57attempted,item58attempted,item59attempted,item60attempted,item61attempted,item62attempted,item63attempted,item64attempted,item65attempted,item66attempted,item67attempted,item68attempted,item69attempted,item70attempted,item71attempted,item72attempted), na.rm=TRUE)))
table(ERAselfknowledge.temp3$ERAtotalattempted)

# create a data frame with those who attempted at least half of the items
accuracy <- subset(ERAselfknowledge.temp3, ERAtotalattempted>35)

# view EI test descriptives
with(accuracy, describe(cbind(ERAtotal,ERApercent,ERAtotalattempted)))

# checking reliability of the emotion recognition ability test
# alpha with whole scale = .92
with(accuracy, psych::alpha(data.frame(item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14,item15,item16,item17,item18,item19,item20,item21,item22,item23,item24,item25,item26,item27,item28,item29,item30,item31,item32,item33,item34,item35,item36,item37,item38,item39,item40,item41,item42,item43,item44,item45,item46,item47,item48,item49,item50,item51,item52,item53,item54,item55,item56,item57,item58,item59,item60,item61,item62,item63,item64,item65,item66,item67,item68,item69,item70,item71,item72), na.rm=TRUE))

# split half (first 36 items vs.last 36 items)
accuracy$ERAtotal1 <- with(accuracy, (rowSums(cbind(item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13,item14,item15,item16,item17,item18,item19,item20,item21,item22,item23,item24,item25,item26,item27,item28,item29,item30,item31,item32,item33,item34,item35,item36), na.rm=TRUE)))
accuracy$ERAtotal2 <- with(accuracy, (rowSums(cbind(item37,item38,item39,item40,item41,item42,item43,item44,item45,item46,item47,item48,item49,item50,item51,item52,item53,item54,item55,item56,item57,item58,item59,item60,item61,item62,item63,item64,item65,item66,item67,item68,item69,item70,item71,item72), na.rm=TRUE)))
# Correlating scores from first and second halves
r1 <- cor(accuracy$ERAtotal1, accuracy$ERAtotal2)
r1
# r=.78
# Adjusting with the Spearman-Brown prophecy formula
(2 * r1) / (1 + r1)

# split half (odd items vs. even items)
accuracy$ERAtotal3 <- with(accuracy, (rowSums(cbind(item1,item3,item5,item7,item9,item11,item13,item15,item17,item19,item21,item23,item25,item27,item29,item31,item33,item35,item37,item39,item41,item43,item45,item47,item49,item51,item53,item55,item57,item59,item61,item63,item65,item67,item69,item71), na.rm=TRUE)))
accuracy$ERAtotal4 <- with(accuracy, (rowSums(cbind(item2,item4,item6,item8,item10,item12,item14,item16,item18,item20,item22,item24,item26,item28,item30,item32,item34,item36,item38,item40,item42,item44,item46,item48,item50,item52,item54,item56,item58,item60,item62,item64,item66,item68,item70,item72), na.rm=TRUE)))
# Correlating scores from even and odd items
r2 <- cor(accuracy$ERAtotal3, accuracy$ERAtotal4)
r2
# r=.84
# Adjusting with the Spearman-Brown prophecy formula
(2 * r2) / (1 + r2)

# view descriptives of EI test scores and self-assessed EI test scores
with(accuracy, describe(cbind(ertselfperformance,ERAtotal,ERApercent)))

# correlating EI test scores with self-assessed EI test scores
with(accuracy, cor.test(ERAtotal,ertselfperformance))

# scoring life satisfaction
with(accuracy, psych::alpha(data.frame(slws_4,slws_5,slws_6,slws_7,slws_8), na.rm=TRUE))
accuracy$lifesat <- with(accuracy,(rowMeans(cbind(slws_4,slws_5,slws_6,slws_7,slws_8),na.rm=TRUE)))
describe(accuracy$lifesat)

# correlating EI test scores and self-assessed EI test scores with criteria
with(accuracy, describe(cbind(ERAtotal,ertselfperformance,lifesat)))
with(accuracy, corr.test(cbind(ERAtotal,ertselfperformance,lifesat)))

# create a final data frame with people who have scores on emotion recognition test, self-views, and life satisfaction
# N=562
accuracy.temp1 <- subset(accuracy, ertselfperformance>-1)
accuracy <- subset(accuracy.temp1, lifesat>-1)

##################################################################
### MAIN ANALYSES FOR SELF-INSIGHT AND OPTIMAL MARGIN OF ILLUSION
##################################################################

# centering data for RSA analyses
accuracy$c.ERAtotal <- accuracy$ERAtotal-36
accuracy$c.ertselfperformance <- accuracy$ertselfperformance-36

# create squared and interaction terms
accuracy$c.ERAtotal2 <- (accuracy$c.ERAtotal)^2
accuracy$c.ertselfperformance2 <- (accuracy$c.ertselfperformance)^2
accuracy$c.ERAtotal_c.ertselfperformance <- accuracy$c.ERAtotal*accuracy$c.ertselfperformance

#### test if full polynomial regression is significant ####
full_model <- lm(lifesat~c.ERAtotal+c.ertselfperformance+c.ERAtotal2+c.ERAtotal_c.ertselfperformance+c.ertselfperformance2, data=accuracy, na.action=na.exclude); summary(full_model)

#### test for self-insight with polynomial regression analyses and RSA ####
RSAlifesat<-RSA(lifesat~c.ERAtotal*c.ertselfperformance,data=accuracy,
                center=FALSE,
                scale=FALSE,
                na.rm=TRUE,
                out.rm=NULL,
                breakline=FALSE, 
                models="default",
                cubic=FALSE,
                verbose=TRUE,
                estimator="MLR",
                se="robust",
                missing='listwise'
)

# see summary and parameters of RSA
RSAlifesat

# plot RSA graph 
plot(RSAlifesat,      
     main = NULL,      
     param   = TRUE,     
     coefs   = TRUE,      
     axes    = c("LOC", "LOIC", "PA1"), 
     project = c("PA1", "LOC"),        
     xlim    = c(-36,36),   
     ylim    = c(-36,36),    
     xlab = "Emotion Recognition Score", 
     ylab = "Self-estimate", 
     zlab = "Life Satisfaction"
)

#### Testing Shifted Ridge Using Lavaan ####

# Polynomial Regression in Lavaan Package
lavaan.model     <- 'lifesat ~ c.ERAtotal + c.ertselfperformance + c.ERAtotal2 + c.ERAtotal_c.ertselfperformance + c.ertselfperformance2'
lavaan.model.fit <- sem(lavaan.model, data = accuracy, se = "robust.sem", estimator = "MLR")
summary(lavaan.model.fit) 
coef(lavaan.model.fit) 

# Bootstrap Polynomial Coefficients
boot.data <- bootstrapLavaan(lavaan.model.fit, #fitted polynomial regression model from above.
                             R = 1000,        #number of bootstrap samples (be patient for larger values, it will take a few moments)
                             FUN = "coef")    
boot.data <- as.data.frame(boot.data) #bootstrapLavaan outputs a matrix, but this line converts it to a dataframe.
names(boot.data) <- c("x", "y", "x.squared", "interaction", "y.squared", "z.variance", "intercept") 

boot.data <- within.data.frame(boot.data, {
  x0.stationary.point <- (y*interaction-2*x*y.squared)/(4*x.squared*y.squared-interaction*interaction) #stationary point x-coordinate 
  y0.stationary.point <- (x*interaction-2*y*x.squared)/(4*x.squared*y.squared-interaction*interaction) #stationary point y-coordinate
  slope.p1            <- (y.squared-x.squared+((x.squared-y.squared)^2+interaction^2)^0.5)/interaction #calculate slope of the first principle axis
  intercept.p1        <- (y0.stationary.point - slope.p1*x0.stationary.point)                          #calculate intercept of the first principle axis
  slope.p2            <- (y.squared-x.squared-((x.squared-y.squared)^2+interaction^2)^0.5)/interaction #calculate slope of the second principle axis
  intercept.p2        <- (y0.stationary.point - slope.p2*x0.stationary.point)                          #calculate intercept of the second principle axis
  lateral.shift.p1    <- (-intercept.p1/(slope.p1+1))                                                  #calculate lateral shift of first principle axis where it intersects line of incongruence (i.e., Y=-X)
  lateral.shift.p2    <- (-intercept.p2/(slope.p2+1))                                                  #calculate lateral shift of second principle axis
})

#### 95% confidence intervals based on bootstrapping polynomial coefficients ####
quantile(boot.data$slope.p1,         c(.05,.95)) #slope of the first principle axis
quantile(boot.data$intercept.p1,     c(.05,.95)) #intercept of the first principle axis
quantile(boot.data$slope.p2,         c(.05,.95)) #slope of the second principle axis
quantile(boot.data$intercept.p2,     c(.05,.95)) #intercept of the second principle axis
quantile(boot.data$lateral.shift.p1, c(.05,.95)) #Lateral shift of the first principal axis
quantile(boot.data$lateral.shift.p2, c(.05,.95)) #Lateral shift of the second principal axis

####################################################
### MAIN ANALYSES FOR ALTERNATIVE MODELS
####################################################

### test of positive self-views only (linear) hypothesis (H2a) ###
##################################################################

# regress adjustment on self-views and abilities
selfviewonly <- lm(lifesat~c.ERAtotal+c.ertselfperformance, data=accuracy)
summary(selfviewonly)

# regress adjustment on self-views ONLY
selfviewonly1 <- lm(lifesat~c.ertselfperformance, data=accuracy)
summary(selfviewonly1)

### test of positive self-views only (curvilinear) hypothesis (H2b) ###
#######################################################################

selfviewcurvilinear <- lm(lifesat~c.ertselfperformance+c.ertselfperformance2, data=accuracy)
summary(selfviewcurvilinear)

### test of high abilities only (linear) hypothesis (H4a) ###
#############################################################

# regress adjustment on abilities
abilityonly <- lm(lifesat~c.ERAtotal+c.ertselfperformance, data=accuracy)
summary(abilityonly)

# regress adjustment on ability ONLY
abilityonly1 <- lm(lifesat~c.ERAtotal, data=accuracy)
summary(abilityonly1)

### test of high abilities only (curvilinear) hypothesis (H4b) ###
##################################################################

abilitycurvilinear <- lm(lifesat~c.ERAtotal+c.ERAtotal2, data=accuracy)
summary(abilitycurvilinear)

### test of positive self-views and high abilities hypothesis (H5) ###
######################################################################

positiveSR <- lm(lifesat~c.ERAtotal+c.ertselfperformance, data=accuracy)
summary(positiveSR)

#########################################
### RUN ANALYSES SUGGESTED BY REVIEWER 2
#########################################

# do analyses as described in "Testing fit patterns ..." (Schonbrodt, 2016)
r1 <- RSA(lifesat ~ c.ERAtotal*c.ertselfperformance, data=accuracy,center=FALSE)
summary(r1)
plot(r1)
aictab(r1, models = c("additive", "onlyx", "onlyx2", "onlyy", "onlyy2", "SQD", "SSQD"), plot=TRUE, digits=5)  # predicted models only
aictab(r1, models = c("full", "additive", "onlyx", "onlyx2", "onlyy", "onlyy2", "SQD", "SSQD", "null"), plot=TRUE, digits=5)  # predicted models plus full and null
compare2(r1, m1="SSQD", m2="full")
getPar(r1, type="coef", model="full")
getPar(r1, type="coef", model="SSQD")
getPar(r1, type="coef", model="SQD")
getPar(r1, type="coef", model="additive")
getPar(r1, type="coef", model="onlyx")
getPar(r1, type="coef", model="onlyx2")
getPar(r1, type="coef", model="onlyy")
getPar(r1, type="coef", model="onlyy2")
confint(r1, model="SSQD")
